{extend name="../template/admin/head.html"}
{block name="title"}
修改文章_{__block__}
{/block}
{block name="content"}
<blockquote class="layui-elem-quote title" style="margin-left:10px;margin-top:10px;"><h2>修改文章</h2></blockquote>
<form class="layui-form" lay-filter="edit">
  <div class="layui-form-item">
    <label class="layui-form-label">文章标题</label>
    <div class="layui-input-block">
      <input type="text" class="layui-input newsName" lay-verify="required" placeholder="请输入文章标题" id="actitle" value="{$artice.title}">
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">自定义属性</label>
      <div class="layui-input-block">
        <input type="checkbox" id="tuijian" class="tuijian" title="推荐" {if condition="($artice.top == 1)"}checked{/if}>
        <input type="checkbox" id="show" class="isShow" title="显示" {if condition="($artice.show == 1)"}checked{/if}>
      </div>
    </div>
    <div class="layui-inline">    
      <label class="layui-form-label">文章作者</label>
      <div class="layui-input-inline">
        <input type="text" class="layui-input newsAuthor" placeholder="文章作者-不填默认是博主" id="author" value="{$artice.author}">
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label">分类</label>
      <div class="layui-input-inline">
        <select name="browseLook" class="newsLook" lay-filter="browseLook" id="selectop">
          {volist name="category" id="category"}
            <option value="{$category.id}">{$category.name}</option>
          {/volist}
        </select>
      </div>
      <div class="layui-btn-group">
        <a class="layui-btn" id="tjclass">添加分类</a>
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">关键字</label>
    <div class="layui-input-block">
      <input type="text" class="layui-input" placeholder="请输入文章关键字-SEO优化" id="keyword" value="{$artice.keyword}">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">内容摘要</label>
    <div class="layui-input-block">
      <textarea placeholder="请输入内容摘要，不输入则自动截取30个字符" class="layui-textarea" id="abstract">{$artice.abstract}</textarea>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">文章内容</label>
    <div class="layui-input-block">
        <div id="editor">
          <p>{$artice.content}</p>
        </div>
    </div>
  </div>
  <input type="hidden" id="acid" value="{$artice.id}">
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn kit-login-btn" lay-submit="submit" lay-filter="addNews" id="addNews">立即发布</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>

{/block}
{block name="jssrc"}'
<script type="text/javascript" src="__public__/wangEditor.min.js"></script>
<script type="text/javascript">
  layui.use(['form', 'layedit', 'laydate'], function() {
    var form = layui.form,
    layer = layui.layer,
    layedit = layui.layedit,
    laydate = layui.laydate;
});
//JavaScript代码区域
layui.use('element', function(){
  var element = layui.element;
});
$('#tjclass').on('click', function(){
  layer.open({
    title:'添加文章分类',
    type: 1,
    area: ['320px', '150px'],
      shadeClose: true, //点击遮罩关闭
      content: ' \<\div style="margin-top:10px; padding-right:10px;">\<\label class="layui-form-label">分类名称：\<\/label>\<\div class="layui-input-block">\<\input type="text" class="layui-input newsName" lay-verify="required" placeholder="分类名称" id="ctname">\<\/div>\<\/div><button class="layui-btn" style="margin-top:10px; margin-left:220px;"lay-submit="submit" lay-filter="tjop" id="wrnav">添加</button>'

    });
});  
//添加分类&修改文章
layui.use('form', function(){
  var form = layui.form;    
  //添加分类
  form.on('submit(tjop)', function(date) {
    //获取输入的值
    text = $('#ctname').val();
    if (text != "") {
      $.ajax({
        type:"POST",
        url:"{:URL('Artice/atadd')}",
        dataType:"json",
        data:{name:text},
        beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#wrnav").attr({"disabled":"disabled"});
            $("#wrnav").addClass("layui-btn-disabled");
            $("#wrnav").html("添加中...");
            layer.load(1);
          },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (XmlHttpRequest.responseText != "") {
              layer.msg('提交失败，请稍后尝试！');
              layer.closeAll('loading');
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("添加");
            }
          },
          success:function(data){
            //1为修改成功
            //2为修改失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("添加");
              $("#selectop").append('<option value="'+date.id+'">'+text+'</option>');
              form.render(); //更新全部
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("添加");
            }
          }
        });
      return false;
    }else{
      layer.msg('所有内容不能为空!');
      return false;
    }
  });
});

layui.use(['form','layedit'], function(){
  var form = layui.form;  
  var layedit = layui.layedit;
  $("#selectop").val({$artice.cid}); layui.form.render();
  //创建编辑器
  var E = window.wangEditor
  var editor = new E('#editor')
  // 配置服务器端地址
  editor.customConfig.uploadImgServer = "{:URL('Artice/imgupload')}";
  editor.customConfig.uploadFileName = 'images';
  editor.customConfig.debug = true
  editor.create()
  //修改文章
form.on('submit(addNews)', function(data){
    id = $('#acid').val();
    title = $('#actitle').val();
    author  = $('#author').val();
    cid     = $('#selectop option:selected').val();
    keyword = $('#keyword').val();
    abstract=$('#abstract').val();
    content = editor.txt.html();
    type = 'artice';
    //判断推荐是否选中
    checktuijian = document.getElementById("tuijian").checked
    checkshow = document.getElementById("show").checked
    //定义全局变量
    var top;
    var show;
    //判断推荐显示选中
    if(checktuijian == true) {
      top = 1;
    }else{
      top = 0;
    }
    
    if(checkshow == true) {
      show = 1;
    }else{
      show = 0;
    }
    
    if (title != "" && content != "") {
       $.ajax({
          type:"post",
          url:"{:URL('Artice/acedit')}",
          dataType:"json",
          data:{id:id,title:title,author:author,cid:cid,keyword:keyword,abstract:abstract,content:content,top:top,show:show,type:type},
            beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#addNews").attr({"disabled":"disabled"});
            $("#addNews").addClass("layui-btn-disabled");
            $("#addNews").html("发布中...");
            layer.load(1);
            },
           error: function (XmlHttpRequest, textStatus, errorThrown) {
              if (XmlHttpRequest.responseText != "") {
                  layer.msg('提交失败，请稍后尝试！');
                  layer.closeAll('loading');
                  $("#addNews").removeClass("layui-btn-disabled");
                  $("#addNews").removeAttr("disabled");  
                  $("#addNews").html("立即发布");
              }
          },
          success:function(data){
            if (data.result == "1") {
              layer.closeAll('loading');
              layer.msg(data.msg);
              setTimeout(function(){
                location.href=data.url;
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
            }
          }
        });
    }else{
      layer.msg('标题和内容不能为空');
    }
    return false;
  });
});
</script>
{/block}